package com.sl.proxy.server.service;

import com.sl.biz.api.JobApi;
import com.sl.biz.domain.request.JobPageParamRpcRequest;
import com.sl.biz.domain.request.JobUpdateByIdRpcRequest;
import com.sl.biz.domain.response.JobVO;
import com.sl.common.page.PageList;
import com.sl.common.timer.CommonTimerTaskRunner;
import com.sl.proxy.server.base.page.PageQuery;
import com.sl.proxy.server.controller.req.system.JobInfoExecuteReq;
import com.sl.proxy.server.controller.req.system.JobInfoListReq;
import com.sl.proxy.server.controller.req.system.JobInfoUpdateEnableStatusReq;
import com.sl.proxy.server.controller.req.system.JobInfoUpdateReq;
import com.sl.proxy.server.controller.res.system.JobInfoExecuteRes;
import com.sl.proxy.server.controller.res.system.JobInfoListRes;
import com.sl.proxy.server.controller.res.system.JobInfoUpdateEnableStatusRes;
import com.sl.proxy.server.controller.res.system.JobInfoUpdateRes;
import com.sl.proxy.server.dao.domain.entity.JobInfoDO;
import com.sl.proxy.server.job.*;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: aoshiguchen
 * @date: 2022/9/5
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class JobInfoService {
    private final JobApi jobApi;

    public PageList<JobInfoListRes> page(PageQuery pageQuery, JobInfoListReq req) {
        JobPageParamRpcRequest jobPageParamRpcRequest = new JobPageParamRpcRequest();
        jobPageParamRpcRequest.setGroupCode("PROXY");
//        jobPageParamRpcRequest.setSearchKey();
//        jobPageParamRpcRequest.setJobStatus();
        jobPageParamRpcRequest.setCurrent(pageQuery.getCurrent());
        jobPageParamRpcRequest.setSize(pageQuery.getSize());
        jobPageParamRpcRequest.setPageIs(true);
        PageList<JobVO> jobVOList = jobApi.page(jobPageParamRpcRequest);

        return PageList.convert(jobVOList, JobInfoListRes.class, (t) -> {
            JobInfoListRes jobInfoListRes = new JobInfoListRes();
            jobInfoListRes.setId(t.getId());
            jobInfoListRes.setDesc(t.getJobDescription());
            jobInfoListRes.setHandler(t.getBeanName());
            jobInfoListRes.setCron(t.getCron());
            jobInfoListRes.setParam(t.getParams());
//            jobInfoListRes.setAlarmEmail();
//            jobInfoListRes.setAlarmDing();
            jobInfoListRes.setEnable(1);
            return jobInfoListRes;
        });

    }

    public List<JobInfoDO> findList() {
        JobPageParamRpcRequest jobPageParamRpcRequest = new JobPageParamRpcRequest();
        jobPageParamRpcRequest.setPageIs(false);
        jobPageParamRpcRequest.setGroupCode("PROXY");
        PageList<JobVO> page = jobApi.page(jobPageParamRpcRequest);

        return PageList.toList(page, JobInfoDO.class, (y) -> {
            JobInfoDO jobInfoDO = new JobInfoDO();
            jobInfoDO.setId(y.getId());
            jobInfoDO.setCron(y.getCron());
            jobInfoDO.setDesc(y.getJobDescription());
//            jobInfoDO.setAlarmEmail();
//            jobInfoDO.setAlarmDing();
            jobInfoDO.setHandler(y.getBeanName());
            jobInfoDO.setParam(y.getParams());
            jobInfoDO.setEnable(1);
            return jobInfoDO;
        });
    }

    public JobInfoUpdateEnableStatusRes updateEnableStatus(JobInfoUpdateEnableStatusReq req) {
        return new JobInfoUpdateEnableStatusRes();
    }

    public JobInfoExecuteRes execute(JobInfoExecuteReq req) {
        jobApi.runJob(req.getId());
        return new JobInfoExecuteRes();
    }


    public JobInfoUpdateRes update(JobInfoUpdateReq req) {
        JobUpdateByIdRpcRequest jobUpdateByIdRpcRequest = new JobUpdateByIdRpcRequest();
        jobUpdateByIdRpcRequest.setId(req.getId());
        jobUpdateByIdRpcRequest.setGroupCode("PROXY");
        jobUpdateByIdRpcRequest.setBeanName(req.getHandler());
        jobUpdateByIdRpcRequest.setJobCode(req.getId());
        jobUpdateByIdRpcRequest.setJobName(req.getDesc());
        jobUpdateByIdRpcRequest.setCron(req.getCron());
        jobApi.updateById(jobUpdateByIdRpcRequest);
        return new JobInfoUpdateRes();
    }
}
